﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace sy2_7
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

                if (!int.TryParse(textBox1.Text, out int precision) || precision < 1)
                {
                    MessageBox.Show("请输入有效的精度值（正整数）！", "输入错误",
                        MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                double threshold = Math.Pow(10, -precision);
                double sum = 0;         
                int n = 0;               
                int denominator = 1;    
                int difference = 0;     

                string formula = "1/1";  
               
                double term = 1.0 / denominator;
                sum += term;
                n = 1;
                while (true)
                { 
                    difference++;
                    denominator += difference;

                   
                    term = 1.0 / denominator;

                    if (term < threshold)
                        break;

                    sum += term;
                    n++;

                    if (n <= 10)
                    {
                        formula += $" + 1/{denominator}";
                    }
                }
                if (n > 10)
                {
                    formula += " + ...";
                }
                textBox1.Text = $"公式：{formula}";
                textBox2.Text = $"Sum = {sum:F15}";  
                textBox3.Text = $"n = {n}";
            }
        }
    }

